package com.laf.manage.controller.admin;

import cn.hutool.core.util.DesensitizedUtil;
import com.laf.common.annotation.Log;
import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.AjaxResult;
import com.laf.common.core.domain.entity.MobileUser;
import com.laf.common.core.page.TableDataInfo;
import com.laf.common.enums.BusinessType;
import com.laf.common.utils.poi.ExcelUtil;
import com.laf.mobile.service.IMobileUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 用户管理Controller
 *
 * @author zian
 * @date 2024-10-30
 */
@Api(tags = "用户管理管理")
@RestController("adminUserController")
@RequestMapping("/admin/user")
public class MobileUserController extends BaseController {
    @Autowired
    private IMobileUserService userService;

    /**
     * 查询用户管理列表
     */
    @ApiOperation("查询用户管理列表")
    @PreAuthorize("@ss.hasPermi('manage:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(MobileUser user) {
        startPage();
        List<MobileUser> list = userService.selectUserList(user);
        for (MobileUser item : list) {
            item.setUserName(DesensitizedUtil.mobilePhone(item.getUserName()));
        }
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @ApiOperation("导出用户管理列表")
    @PreAuthorize("@ss.hasPermi('manage:user:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MobileUser user) {
        List<MobileUser> list = userService.selectUserList(user);
        ExcelUtil<MobileUser> util = new ExcelUtil<MobileUser>(MobileUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @ApiOperation("获取用户管理详细信息")
    @PreAuthorize("@ss.hasPermi('manage:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return success(userService.getById(userId));
    }

    /**
     * 新增用户管理
     */
    @ApiOperation("新增用户管理")
    @PreAuthorize("@ss.hasPermi('manage:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MobileUser user) {
        return toAjax(userService.save(user));
    }

    /**
     * 修改用户管理
     */
    @ApiOperation("修改用户管理")
    @PreAuthorize("@ss.hasPermi('manage:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MobileUser user) {
        return toAjax(userService.updateById(user));
    }

    /**
     * 删除用户管理
     */
    @ApiOperation("删除用户管理")
    @PreAuthorize("@ss.hasPermi('manage:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(userService.removeByIds(Arrays.asList(userIds)));
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    @PreAuthorize("@ss.hasPermi('manage:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MobileUser user) {
        return toAjax(userService.lambdaUpdate()
                .eq(MobileUser::getUserId, user.getUserId())
                .set(MobileUser::getStatus, user.getStatus())
                .update());
    }

}
